home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 3 / Amiga Format CD03 (1996-07-04)(Future Publishing)(GB)(Track 1 of 6)[!][issue 1996-08].iso / pd / utilities / tinymeter / source / tinymeter_main / initclean.c < prev    next >
C/C++ Source or Header  |  1996-05-20  |  12KB  |  431 lines

  1. #include <exec/types.h>
  2. #include <exec/memory.h>
  3. #include <dos/dos.h>
  4. #include <dos/dosextens.h>
  5. #include <intuition/intuition.h>
  6. #include <intuition/gadgetclass.h>
  7. #include <intuition/intuitionbase.h>
  8. #include <intuition/classusr.h>
  9. #include <intuition/imageclass.h>
  10. #include <intuition/gadgetclass.h>
  11. #include <intuition/cghooks.h>
  12. #include <intuition/icclass.h>
  13. #include <intuition/classes.h>
  14. #include <intuition/sghooks.h>
  15. #include <intuition/screens.h>
  16. #include <datatypes/datatypesclass.h>
  17. #include <datatypes/datatypes.h>
  18. #include <datatypes/pictureclass.h>
  19. #include <graphics/gfxbase.h>
  20. #include <graphics/text.h>
  21. #include <graphics/gfxmacros.h>
  22. #include <utility/tagitem.h>
  23. #include <utility/hooks.h>
  24. #include <string.h>
  25. #include <clib/macros.h>
  26. #include "gaugeclass.h"
  27. #include "launchclass.h"
  28. #include "tinymeter.h"
  29.  
  30. extern struct IntuitionBase *IntuitionBase;
  31.  
  32. struct Gadget my_drag=
  33. {
  34.     NULL,
  35.     0,1,
  36.     0,0,
  37.     GFLG_GADGHNONE,
  38.     GACT_IMMEDIATE,
  39.     GTYP_SYSGADGET|GTYP_WDRAGGING,
  40.     0,
  41.     0,
  42.     0,
  43.  
  44.     0,
  45.     0,
  46.     0xFFF2,
  47.     0
  48. };
  49.  
  50. struct Gadget my_close=
  51. {
  52.     NULL,
  53.     0,0,
  54.     16,16,
  55.     NULL,
  56.     GACT_IMMEDIATE,
  57.     GTYP_SYSGADGET|GTYP_CLOSE,
  58.     0,
  59.     0,
  60.     0,
  61.  
  62.     0,
  63.     0,
  64.     0xFFF1,
  65.     0
  66. };
  67.  
  68. struct Gadget my_size=
  69. {
  70.     NULL,
  71.     0,0,
  72.     16,16,
  73.     GFLG_GADGHNONE,
  74.     GACT_IMMEDIATE,
  75.     GTYP_SYSGADGET|GTYP_SIZING,
  76.     0,
  77.     0,
  78.     0,
  79.  
  80.     0,
  81.     0,
  82.     0xFFF3,
  83.     0
  84. };
  85.  
  86. new_window_size(struct tm_sys_set *set,struct tm_data *data)
  87. {
  88.     struct Window   *win=data->win;
  89.     my_drag.Width   =win->Width;
  90.     my_drag.Height  =win->Height-17;
  91.     my_size.TopEdge =win->Height-16;
  92.     my_size.LeftEdge=win->Width -16;
  93. }
  94.  
  95. struct TextFont *loadFont(char *name, UWORD size)
  96. {
  97.     struct  TextFont     *tf;
  98.     struct  TextAttr      my_text_attr;
  99.  
  100.     my_text_attr.ta_Name =(UBYTE *)name;
  101.     my_text_attr.ta_YSize=(UWORD)size;
  102.     my_text_attr.ta_Flags=FPF_DISKFONT | FPB_PROPORTIONAL;
  103.  
  104.     tf=(struct TextFont *)OpenDiskFont((struct TextAttr *)&my_text_attr);
  105.     if(!tf) if(!(tf=(struct TextFont *)OpenTopaz())) return(0L);
  106.     return(tf);
  107. }
  108.  
  109. struct Window *open_new_window(struct tm_sys_set *set,struct tm_data *data, UWORD ysiz)
  110. {
  111.     struct Window *window;
  112.  
  113.     if((window=(struct Window *)OpenWindowTags(0,
  114.     WA_Left,            set->x_pos,
  115.     WA_Top,             set->y_pos,
  116.     WA_Width,           set->x_siz,
  117.     WA_MaxWidth,        4096,
  118.     WA_MaxHeight,       ysiz,
  119.     WA_MinHeight,       ysiz,
  120.     WA_Height,          ysiz,
  121.     WA_MinWidth,        data->min_x_size,
  122.     WA_Flags,           (set->win_backfront!=win_back ? 0 : WFLG_BACKDROP )|WFLG_BORDERLESS|WFLG_RMBTRAP|WFLG_REPORTMOUSE,
  123.     WA_PubScreen,       data->scr,
  124.     TAG_DONE)))
  125.     {
  126.     if(data->appport=(struct MsgPort *)CreateMsgPort(0L)) data->appwin=(struct AppWindow *)AddAppWindow(0L, 0L, window, data->appport , 0L);
  127.     my_drag.Width=   window->Width;
  128.     my_drag.Height=  window->Height-17;
  129.     my_size.TopEdge= window->Height-16;
  130.     my_size.LeftEdge=window->Width -16;
  131.     if(set->win_move==win_normal)
  132.     {
  133.         my_drag.NextGadget=NULL;
  134.         my_size.NextGadget=NULL;
  135.         my_close.NextGadget=NULL;
  136.         AddGadget(window,&my_drag, (UWORD)0);
  137.         AddGadget(window,&my_size, (UWORD)0);
  138.         AddGadget(window,&my_close,(UWORD)0);
  139.     }
  140.     ModifyIDCMP(window,IDCMP_NEWSIZE|IDCMP_CLOSEWINDOW|IDCMP_CHANGEWINDOW|IDCMP_SIZEVERIFY|IDCMP_VANILLAKEY|IDCMP_MOUSEBUTTONS|IDCMP_GADGETDOWN|IDCMP_GADGETUP);
  141.     return(window);
  142.     }
  143.     return(0L);
  144. }
  145.  
  146. struct Window *snapBackground(struct tm_sys_set *set,struct tm_data *data)
  147. {
  148.     struct Window *win=data->win;
  149.     UWORD x,y,xs,ys;
  150.     x=win->LeftEdge;
  151.     y=win->TopEdge;
  152.     xs=win->Width;
  153.     ys=win->Height;
  154.     set->x_pos=x;
  155.     set->y_pos=y;
  156.     set->x_siz=xs;
  157.     if(data->bg_bm) FreeBitMap(data->bg_bm); data->bg_bm=0;
  158.     if(data->bg_bm=(struct BitMap *)AllocBitMap(xs,ys,data->scr->RastPort.BitMap->Depth,0,0))
  159.     {
  160.     RemoveGList(win,win->FirstGadget,-1);
  161.     if(data->appwin)  RemoveAppWindow(data->appwin);
  162.     if(data->appport) DeleteMsgPort  (data->appport);
  163.     CloseWindow(win);
  164.     BltBitMap(data->scr->RastPort.BitMap,x,y,data->bg_bm,0,0,xs,ys,0xc0,0xff,0);
  165.     return((struct Window *)open_new_window(set,data,ys));
  166.     }
  167.     return(0L);
  168. }
  169.  
  170. void CopyTiledBitMap(struct BitMap *Src,WORD SrcSizeX,WORD SrcSizeY,WORD DstSizeX,WORD DstSizeY ,struct BitMap *Dst)
  171. {
  172.     WORD PosX,
  173.      PosY;
  174.     WORD SizeX,
  175.      SizeY;
  176.     for (PosX = 0,SizeX = MIN(SrcSizeX,DstSizeX);PosX<DstSizeX;)
  177.     {
  178.     for (PosY = 0,SizeY = MIN(SrcSizeY,DstSizeY);PosY<DstSizeY;)
  179.     {
  180.         BltBitMap(Src,0,0,Dst,PosX,PosY,SizeX,SizeY,0xC0,0xff,0L);
  181.         PosY += MIN(SizeY,DstSizeY-PosY);
  182.         SizeY = MIN(SizeY,DstSizeY-PosY);
  183.     }
  184.     PosX += MIN(SizeX,DstSizeX-PosX);
  185.     SizeX = MIN(SizeX,DstSizeX-PosX);
  186.     }
  187. }
  188.  
  189. Object *LoadImage(char *file, struct Screen *scr)
  190. {
  191.     Object              *dt_obj;
  192.  
  193.     if (dt_obj = (Object *)NewDTObject(file,
  194.     DTA_SourceType       ,DTST_FILE,
  195.     DTA_GroupID          ,GID_PICTURE,
  196.     PDTA_Remap           ,TRUE,
  197.     PDTA_Screen          ,scr,
  198.     PDTA_FreeSourceBitMap,TRUE,
  199.     OBP_Precision        ,PRECISION_IMAGE,
  200.     TAG_DONE))
  201.     {
  202.     if ( DoDTMethod(dt_obj,NULL,NULL,DTM_PROCLAYOUT,NULL,1))
  203.     {
  204.         return(dt_obj);
  205.     }
  206.     }
  207.     return(NULL);
  208. }
  209.  
  210. fileBackground(struct tm_sys_set *set,struct tm_data *data)
  211. {
  212.     struct Window       *win=data->win;
  213.     struct BitMap       *work;
  214.     struct BitMapHeader *header;
  215.     UWORD xs,ys;
  216.  
  217.     if (data->bg_bm){ FreeBitMap(data->bg_bm); data->bg_bm=0L; }
  218.     if (data->dt_object = LoadImage(&set->bg_picture[0],data->scr))
  219.     {
  220.     GetDTAttrs(data->dt_object,PDTA_BitMapHeader,&header,PDTA_DestBitMap,&work,TAG_DONE);
  221.     if (work==FALSE) GetDTAttrs(data->dt_object,PDTA_BitMap,&work,TAG_DONE);
  222.  
  223.     xs=win->Width;
  224.     ys=win->Height;
  225.     if(data->bg_bm=(struct BitMap *)AllocBitMap(xs,ys,work->Depth,0,0))
  226.     {
  227.         CopyTiledBitMap(work,header->bmh_Width,header->bmh_Height,xs,ys,data->bg_bm);
  228.     }
  229.     }
  230. }
  231.  
  232. struct Window *openWindow(struct tm_sys_set *set,struct tm_data *data, Class *gclass, Class *lclass)
  233. {
  234.     UWORD               y_siz,
  235.             line,
  236.             i,
  237.             tmp;
  238.     struct tm_gau_set   *many;
  239.     struct RastPort     *tmpras;
  240.     BOOL                change;
  241.     if( data->scr=(struct Screen *)LockPubScreen((char *)&set->pub_name[0]))
  242.     {
  243.     data->on_public=TRUE;
  244.     }
  245.     else
  246.     {
  247.     data->scr=(struct Screen *)IntuitionBase->FirstScreen;
  248.     data->on_public=FALSE;
  249.     }
  250.     data->labelpos=0;
  251.     if(tmpras=(struct RastPort *)pAllocVec(sizeof(struct RastPort)))
  252.     {
  253.     InitRastPort(tmpras);
  254.     for(i=0;i<data->num_of_gaug;i++)
  255.     {
  256.         data->gauge_y_size[i]=0;
  257.         data->gauge_y_size_falling[i]=0;
  258.     }
  259.     for(i=0,many=data->list;i<data->num_of_gaug;i++)
  260.     {
  261.         if(many->type!=typ_none)
  262.         {
  263.         line=(i/(set->colums));
  264.         if(data->Font[i]=loadFont(many->font,many->font_size))
  265.             data->gauge_y_size[line]=MAX(data->gauge_y_size[line],data->gauge_y_size_falling[i]=(data->Font[i]->tf_YSize*(many->size_y+100))/100);
  266.         if((many->gauge_type!=typ_histmeter)&&(many->type!=typ_clock_)&&(many->type!=typ_simplelauncher))
  267.         {
  268.             SetFont(tmpras,data->Font[i]);
  269.             tmp=TextLength(tmpras,many->label,my_strlen(many->label));
  270.             if(tmp>data->labelpos)data->labelpos=tmp;
  271.         }
  272.         }
  273.         else data->Font[i]=0;
  274.         many=many->next;
  275.     }
  276.     data->labelpos+=4;
  277.     pFreeVec(tmpras);
  278.     data->num_of_rows   =((data->num_of_gaug-1)/set->colums)+1;
  279.     data->min_x_size    =((data->labelpos+8)*set->colums)+(set->win_space_x*(set->colums-1))+(set->win_border_x<<1);
  280.     if(data->min_x_size>set->x_siz)set->x_siz=data->min_x_size;
  281.     data->gauge_x_size  =(set->x_siz-(set->win_border_x<<1)-((set->colums-1)*set->win_space_x))/set->colums;
  282.     allocGadgets(set,data,gclass,lclass);
  283.     change=FALSE;
  284.     for(i=0,many=data->list;i<data->num_of_gaug;i++)
  285.     {
  286.         if(data->gdg[i])
  287.         {
  288.         ULONG   height;
  289.         line=(i/(set->colums));
  290.         switch (many->type)
  291.         {
  292.             case    typ_simplelauncher:
  293.                 GetAttr(LAU_ImgHeight,data->gdg[i],&height);
  294.                 data->gauge_y_size[line]        = MAX(data->gauge_y_size[line],     height + 6);
  295.                 data->gauge_y_size_falling[i]   = MAX(data->gauge_y_size_falling[i],height + 6);
  296.                 SetAttrs(data->gdg[i],GA_Height,data->gauge_y_size_falling[i],TAG_DONE);
  297.                 change=TRUE;
  298.                 break;
  299.             case    typ_iconlauncher:
  300.                 {
  301.                 struct  lau_entry       *foobar;
  302.                 struct  BitMapHeader    *bmhd;
  303.                 ULONG                   height=0;
  304.                 GetAttr(LAU_List,data->gdg[i],&foobar);
  305.                 if(foobar)
  306.                 {
  307.                     do
  308.                     {
  309.                     if(foobar->img)
  310.                     {
  311.                         GetDTAttrs(foobar->img,PDTA_BitMapHeader,&bmhd,TAG_DONE);
  312.                         if(bmhd)
  313.                         {
  314.                         height=MAX(height,bmhd->bmh_Height);
  315.                         }
  316.                     }
  317.                     }
  318.                     while(foobar=foobar->next);
  319.                     data->gauge_y_size[line]        = MAX(data->gauge_y_size[line],     height);
  320.                     data->gauge_y_size_falling[i]   = MAX(data->gauge_y_size_falling[i],height);
  321.                     SetAttrs(data->gdg[i],GA_Height,height,TAG_DONE);
  322.                     change=TRUE;
  323.                 }
  324.                 }
  325.                 break;
  326.         }
  327.         }
  328.         many=many->next;
  329.     }
  330.     if(change)
  331.     {
  332.         WORD y_pos,j;
  333.         for(i=0;i<data->num_of_gaug;i++)
  334.         {
  335.         if(data->gdg[i])
  336.         {
  337.             line=(i/(set->colums));
  338.             if(set->lay_falling)
  339.             {
  340.             y_pos=set->win_border_y+(line*set->win_space_y);
  341.             for(j=(i-(line*set->colums));j<i;j+=set->colums) y_pos+=data->gauge_y_size_falling[j];
  342.             }
  343.             else
  344.             {
  345.             y_pos= set->win_border_y+(line*set->win_space_y)+((data->gauge_y_size[line]-data->gauge_y_size_falling[i])>>1);
  346.             for(j=0;j<line;j++) y_pos+=data->gauge_y_size[j];
  347.             }
  348.             data->gauge_y_pos[i]=y_pos;
  349.             SetAttrs(data->gdg[i],GA_Top,y_pos,TAG_DONE);
  350.         }
  351.         }
  352.     }
  353.     if(set->lay_falling)
  354.     {
  355.         UWORD old_siz,j;
  356.         for(i=0,y_siz=0;i<set->colums;i++)
  357.         {
  358.         old_siz=y_siz;
  359.         for(j=i,y_siz=0;j<data->num_of_gaug;j+=set->colums) y_siz+=data->gauge_y_size_falling[j];
  360.         if(y_siz>old_siz) old_siz=y_siz;
  361.         }
  362.         y_siz=old_siz;
  363.     }
  364.     else for(i=0,y_siz=0;i<data->num_of_rows;i++)y_siz+=data->gauge_y_size[i];
  365.     y_siz+=(set->win_border_y<<1)+(set->win_space_y*(data->num_of_rows-1));
  366.     if(data->win=(struct Window *)open_new_window(set,data,y_siz))
  367.     {
  368.         ULONG   list;
  369.         data->bg_color      =obtainPen(data->scr,&set->bg_color);
  370.         data->bright_color  =obtainPen(data->scr,&set->bright_color);
  371.         data->dark_color    =obtainPen(data->scr,&set->dark_color);
  372.         switch (set->bg_type)
  373.         {
  374.         case    bg_file:
  375.             fileBackground(set,data);
  376.             break;
  377.         case    bg_snap:
  378.             data->win=snapBackground(set,data);
  379.             break;
  380.         }
  381.         drawBackground(set,data);
  382.         for(i=0;i<data->num_of_gaug;i++) if(data->gdg[i])
  383.         {
  384.         GetAttr(LAU_List,data->gdg[i],&list);
  385.         if(list) AddGList(data->win,data->gdg[i],0,1,NULL);
  386.         else     AddGList(data->win,data->gdg[i],-1,1,NULL);
  387.         }
  388.         RefreshGList(data->gdg[i],data->win,NULL,-1);
  389.         return(data->win);
  390.     }
  391.     }
  392.     return(0L);
  393. }
  394.  
  395. closeWindow(struct tm_sys_set *set,struct tm_data *data)
  396. {
  397.     int i;
  398.     if(!set->bg_color.pen)     ReleasePen     (data->scr->ViewPort.ColorMap,data->bg_color);
  399.     if(!set->bright_color.pen) ReleasePen     (data->scr->ViewPort.ColorMap,data->bright_color);
  400.     if(!set->dark_color.pen)   ReleasePen     (data->scr->ViewPort.ColorMap,data->dark_color);
  401.     if(data->appwin)           RemoveAppWindow(data->appwin);
  402.     if(data->appport)          DeleteMsgPort  (data->appport);
  403.     if(data->win)
  404.     {
  405.     RemoveGList(data->win,data->win->FirstGadget,-1);
  406.     CloseWindow(data->win);
  407.     data->win=0L;
  408.     }
  409.     for(i=0;i<data->num_of_gaug;i++) if(data->Font[i])
  410.     {
  411.     CloseFont(data->Font[i]);
  412.     data->Font[i]=0L;
  413.     }
  414.     if(data->on_public)
  415.     {
  416.     UnlockPubScreen((char *)&set->pub_name[0]);
  417.     data->on_public=FALSE;
  418.     }
  419.     if(data->dt_object)
  420.     {
  421.     DisposeDTObject((Object *)data->dt_object);
  422.     data->dt_object=0L;
  423.     }
  424.     if(data->bg_bm)
  425.     {
  426.     FreeBitMap(data->bg_bm);
  427.     data->bg_bm=0L;
  428.     }
  429.     removeGadgets(set,data);
  430. }
  431.